<script >
import CRUD, { presenter, header, form, crud } from '@/components/Crud/customCrud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/PaginationCustom.vue'
import crudOperation from '@crud/CRUD.operation.vue'
import udOperation from '@crud/UD.operation.vue'
import ImageSelect from '@/components/ImageSelect/index.vue'
import SubjectSetting from '@/views/system/subject/setting.vue'
const defaultForm = {
  id: null,
  nickName: '',
  icon: '',
  content: '',
  state: true,
  px: 0
}
export default {
  name: 'SubjectIndex',
  components: { udOperation, crudOperation, rrOperation, pagination, ImageSelect, SubjectSetting },
  cruds() {
    return CRUD({
      title: '主题服务',
      url: '/rest/api/manager/v2/service/subject',
      props: {
        searchToggle: true
      }
    })
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    return {
      baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
      enable: [{
        id: 'k-1',
        label: '启用',
        value: true
      }, {
        id: 'k-2',
        label: '禁用',
        value: false
      }]
    }
  },
  methods: {
    setting(data) {
      this.$refs.setting.openSetting(data.id)
    }
  }
}
</script>

<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.blurry"
          clearable
          size="small"
          placeholder="请输入数据源名称"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation />
    </div>
    <!--列表项-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      highlight-current-row
      style="width: 100%;"
      :data="crud.data"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="nickName" label="中文名称" />
      <el-table-column prop="icon" label="主题图标">
        <template slot-scope="scope">
          <el-image
            style="height: 32px;width: 32px;"
            :src="baseURL + scope.row.icon.substring(1, scope.row.icon.length)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="content" label="描述" />
      <el-table-column
        label="操作"
        width="200px"
        align="center"
        fixed="right"
      >
        <template v-slot="scope">
          <udOperation :data="scope.row">
            <el-button size="mini" type="primary" icon="el-icon-setting" @click="setting(scope.row)" />
          </udOperation>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
    <!--  表单项  -->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="700px"
    >
      <el-form ref="form" :inline="true" :model="form" size="small" label-width="100px">
        <el-form-item
          :rules="[{ required: true, message: '请输入主题名称', trigger: 'blur' }]"
          label="主题名称"
          prop="nickName"
        >
          <el-input v-model="form.nickName" placeholder="请输入主题名称" style="width: 500px" />
        </el-form-item>
        <el-form-item
          label="主题图标"
          prop="icon"
          :rules="[{ required: true, message: '请选择主题图标', trigger: 'change' }]"
        >
          <el-popover
            placement="bottom-start"
            trigger="click"
            width="500"
          >
            <el-button slot="reference" size="small" style="padding: 0">
              <el-image
                v-if="form.icon"
                style="height: 32px;width: 32px;"
                :src="baseURL + form.icon.substring(1, form.icon.length)"
              />
              <i v-else style="font-size: 20px;height: 32px;width: 32px;line-height: 32px" class="el-icon-plus" />
            </el-button>
            <image-select ref="iconSelect" v-model="form.icon" />
          </el-popover>
        </el-form-item>
        <el-form-item
          :rules="[{ required: true, message: '请输入主题描述', trigger: 'blur' }]"
          label="主题描述"
          prop="content"
        >
          <el-input v-model="form.content" type="textarea" style="width: 500px" placeholder="请输入主题描述" />
        </el-form-item>
        <el-form-item
          :rules="[{ required: true, message: '请选择状态', trigger: 'change' }]"
          label="状态"
          prop="state"
        >
          <el-radio-group
            v-model="form.state"
          >
            <el-radio
              v-for="item in enable"
              :key="item.id"
              :label="item.value"
            >{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="排序"
          :rules="[{ required: true, message: '请输入排序', trigger: 'blur' }]"
          prop="px"
        >
          <el-input-number v-model.number="form.px" :min="1" controls-position="right" style="width: 100%;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <!--  设置  -->
    <subject-setting ref="setting" />
  </div>
</template>

<style scoped>

</style>
